package sell;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/6/8.
 */
@WebServlet("/lunboadd")  /*@MultipartConfig*/
public class LunboAdd extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String action =req.getParameter("action");
        DbTest db = new DbTest();

        String atime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        String path =req.getServletContext().getRealPath("upload/lunbo");

        switch (action){
            case "add":
                String image =null;
                File f = new File(path);
                if(!f.exists()){
                    f.mkdirs();
                }

               boolean siMultipart= ServletFileUpload.isMultipartContent(req);

                if(siMultipart) {
                    DiskFileItemFactory ff = new DiskFileItemFactory();
                    ServletFileUpload upload = new ServletFileUpload(ff);
                    try {
                        Map<String, List<FileItem>> maps = upload.parseParameterMap(req);

                        //直接接表单uesername字符串的值
                        String title=maps.get("name").get(0).getString("utf-8");
                        String url=maps.get("link").get(0).getString("utf-8");
                        String flag=maps.get("state").get(0).getString("utf-8");

                       /* //直接接checkbox多选表单
                        *//**//*List<FileItem> item = maps.get("state");
                        for(FileItem fi : item){
                            System.out.println(fi.getString("utf-8")+"!!!!!!!!!!!");
                        }*//**//**/

                        //直接接userfile 单文件
                        FileItem ft = maps.get("filename").get(0);

                        if (!ft.getName().equals("")) {
                            image =ft.getName();

                            ft.write(new File(path, image));
                        }


                        int num=db.insert("insert into change_list2 values(null,?,?,?,?,?)", new Object[]{title, image,url,atime,flag });
                    } catch (FileUploadException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                /*req.getRequestDispatcher("lunboadd?action=show").forward(req,resp);*/
                break;
                case "show":
                     List<Map<String, Object>> list= db.query("lunbo_list" ,"*","where 1=1","","");

                    req.setAttribute("list",list);
                    req.getRequestDispatcher("sell/sll_lunbolist.jsp").forward(req,resp);
                    break;
                case "update":
                    String id =req.getParameter("id");
                    String flag = req.getParameter("f");
                    db.update("update lunbo_list set lunbo_state=? where id="+id,new Object[]{flag});
                    req.getRequestDispatcher("lunboadd?action=show").forward(req,resp);
                    break;
                case "delete":
                    String idd=req.getParameter("id");
                    String im=req.getParameter("image");
                    db.delete("lunbo_list","where id="+idd);
                    File fff = new File(path,im);
                    if(fff.exists()){
                        fff.delete();
                    }
                    req.getRequestDispatcher("lunboadd?action=show").forward(req,resp);
                    break;
                case "index":
                    List<Map<String, Object>> list02= db.query("change_list2" ,"*","where change_list2_state='y'","","");

                    List<Map<String, Object>> list03= db.query("fuzhuang_xiao" ,"*","where 1=1","","");

                    for(Map<String,Object> mm: list03){
                        List<Map<String, Object>> list04= db.query("fuzhang_img" ,"*"," where img_pid="+mm.get("xiao_pid"),"","limit 1");
                        mm.put("img",list04.get(0).get("img_name"));
                        System.out.println(list04.get(0).get("img_name"));
                    }


                    req.setAttribute("list", list02);
                    req.setAttribute("list03",list03);
                    System.out.println(list03.size()+"%%%%%");
                    req.getRequestDispatcher("index01.jsp").forward(req,resp);

                    break;
            default:
                    break;
        }
    }
}
